﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SideMenu : MonoBehaviour
{
  //the szie of the side window
  public Rect sideWindow = new Rect(Screen.width + Screen.width / 2f, 0, Screen.width - Screen.width / 3, Screen.height);

  //standard button rectangle
  private Rect buttonRect = new Rect(0, 0, 100, 25);

  //basic window for all the pop ups
  private Rect popUpBox = new Rect(Screen.width / 2, Screen.height / 2, 200, 400);

  //Holds the picture of the last building placed
  public List<Texture2D> LastPlaced = new List<Texture2D>();

  //controls the score window
  bool scoreWindowControl = false;

  //controls the reset window
  bool resetWindowControl = false;

  //controls the reccomendation window
  bool reccomendatinWindowControl = false;

  //controls the maximum number of pictures in LastPlaced list
  public int maxPics = 1;
  private Texture2D testImage;

  //holds the strings for the recomendation windwo
  private Queue<string> reccomendationLog = new Queue<string>();

  //place holder string for the reccomendation window
  private string reccomendationText = "";

  //sets the maximum number of lines that reccomendations in the window at once
  public int maxReccomendations = 1;

  //taken from ClientRecommendationsManager
  GameNetworkManager gameRPC;

  //to tempoarly hold recommendations
  string tempRec = "";


  double playerScore = 0;


  public double score
  {
    get
    {
      return playerScore;
    }
    set
    {
      playerScore = value;
    }
  }

  public string recText
  {
    get
    {
      return reccomendationText;
    }
    set
    {
      reccomendationText = value;
    }
  }

  void Start()
  {

    this.gameRPC = GameObject.FindGameObjectWithTag("GameNetworkManager").GetComponent<GameNetworkManager>();
    gameRPC.sideMenu = this;
    score = gameRPC.gameScore;

  }


  void OnGUI()
  {
    //main side menu window
    GUI.Window(1, sideWindow, SideWindow, "SideMenu");

    //call for the score window
    if (scoreWindowControl)
    {
      GUI.Window(2, popUpBox, ScoreWindow, "This is your score");
    }

    //call for the reset window
    if (resetWindowControl)
    {
      GUI.Window(3, popUpBox, ResetGame, "Reset Game");
    }

    //call for the reccomendation window
    if (reccomendatinWindowControl)
    {
      GUI.Window(4, popUpBox, RecommendationWindow, "Reconmmendation");
    }
  }

  void SideWindow(int windowId)
  {
    GUILayout.BeginVertical();
    //brings up the window to show the score
    if (GUILayout.Button("Show my Score"))
    {
      scoreWindowControl = true;
      resetWindowControl = false;
      reccomendatinWindowControl = false;
    }
    //shows the window to reset the game
    if (GUILayout.Button("Reset Game"))
    {
      resetWindowControl = true;
      scoreWindowControl = false;
      reccomendatinWindowControl = false;
    }
    //brings up the recommendation window
    if (GUILayout.Button("Recommendation"))
    {
      reccomendatinWindowControl = true;
      scoreWindowControl = false;
      resetWindowControl = false;
    }

    //makes sure an image is in teh LastPlaced list before printing to the screen
    if (testImage != null)
    {
      GUI.DrawTexture(new Rect(0, sideWindow.height / 3, sideWindow.width * .9f, (sideWindow.height / 3) * .9f), LastPlaced[0]);
    }
    GUILayout.EndVertical();
  }

  //controls the score window
  void ScoreWindow(int windowId)
  {
    GUILayout.Label("The score is: " + playerScore.ToString());
    GUILayout.Label("Reset buttong goes here");
    if (GUILayout.Button("Close window"))
    {
      scoreWindowControl = false;
    }

  }

  //window the shows the remmendations
  void RecommendationWindow(int windowId)
  {
    //taken from ClientRecommendationsManager
    if (GUILayout.Button("Get Recommendation"))
    {
      print("Recommendation requested");
      gameRPC.ClientRequestRecommendation();
      //tempRec = gameRPC.clientRecommendationManager.ToString();
      //print(tempRec);
      //reccomendationLog.Enqueue(tempRec);
    }
    GUILayout.Label(reccomendationText);
    if (GUILayout.Button("Close window"))
    {
      reccomendatinWindowControl = false;
    }
  }

  //window to reset the game
  void ResetGame(int windowId)
  {
    GUILayout.Label("Instuctions for clearing the board");
    GUILayout.Label("if statement to reset the game when the board is clear");
    if (GUILayout.Button("Close window"))
    {
      resetWindowControl = false;
    }
  }

  //adds textures to the Lastplaced list
  public void AddTexture(Texture2D buildingImage)
  {

    LastPlaced.Add(buildingImage);
    testImage = LastPlaced[0];
    if (LastPlaced.Count > maxPics)
    {
      LastPlaced.Remove(LastPlaced[0]);
    }
  }


  //adds new recammendations to the reccomendationLog list
  public void AddReccomendation(string reccomendatinoString)
  {
    //removes teh oldest reccomendation int the queue
    if (reccomendationLog.Count >= maxReccomendations)
    {
      reccomendationLog.Dequeue();
    }

    //add in the last reccomendation
    reccomendationLog.Enqueue(reccomendatinoString);
    reccomendationText = "";

    //adds a new string to reccomendationText and starts a new line
    foreach (string reccomendation in reccomendationLog)
    {
      reccomendationText += reccomendation;
      reccomendationText += "\n";
    }
  }
}